import pandas as pd
import matplotlib.pyplot as plt

# 画图工具

# 读取CSV文件
acc_df = pd.read_csv('1m_CNN_tra_steep_acc_0507.csv')
loss_df = pd.read_csv('1m_CNN_tra_steep_loss_0507.csv')

# 将字符串转换为适当的数据类型
acc_df['acc'] = acc_df['acc'].astype(float)
loss_df['loss'] = loss_df['loss'].astype(float)

# 假设每个epoch对应800个steps，生成epoch列
acc_df['epoch'] = (acc_df['step'] // 800).astype(int)
loss_df['epoch'] = (loss_df['step'] // 800).astype(int)

# 绘制图表，设置大小为1920x1080
fig, ax1 = plt.subplots(figsize=(1920/100, 1080/100))  # 使用英寸为单位，需要除以100进行转换


# 绘制准确率曲线
ax1.plot(acc_df['epoch'], acc_df['acc'], marker='o', color='blue', label='Accuracy')
ax1.set_xlabel('Epoch')
ax1.set_ylabel('Accuracy', color='blue')
ax1.tick_params('y', colors='blue')

# 创建第二个Y轴来绘制损失
ax2 = ax1.twinx()
ax2.plot(loss_df['epoch'], loss_df['loss'], marker='x', color='red', label='Loss')
ax2.set_ylabel('Loss', color='red')
ax2.tick_params('y', colors='red')

# 添加图例
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines + lines2, labels + labels2, loc='upper left')

# 添加标题
plt.title('Model Accuracy and Loss over Epochs')

# 显示网格
ax1.grid(True)
ax2.grid(True)

# 保存图表为SVG格式
plt.savefig('model_accuracy_loss.svg', format='svg',  dpi=192)

# 显示图表
plt.show()